<?php
namespace App\Http\Extend;

use Firebase\JWT\JWT;
use Firebase\JWT\Key;

class Token
{
//获取token
public function genToken($user){
    $key = 'example_key';
    $payload = [
        'iss' => 'http://example.org',
        'aud' => 'http://example.com',
        'iat' => 1356999524,
        'nbf' => 1357000000,
        'data'=>[
            'user_id'=>$user['id']
        ]
    ];

    /**
     * IMPORTANT:
     * You must specify supported algorithms for your application. See
     * https://tools.ietf.org/html/draft-ietf-jose-json-web-algorithms-40
     * for a list of spec-compliant algorithms.
     */
    $jwt = JWT::encode($payload, $key, 'HS256');

    return $jwt;
}
//解析token
public function deToken($token){
    $key = 'example_key';
    /**
     * IMPORTANT:
     * You must specify supported algorithms for your application. See
     * https://tools.ietf.org/html/draft-ietf-jose-json-web-algorithms-40
     * for a list of spec-compliant algorithms.
     */
    $decoded = JWT::decode($token, new Key($key, 'HS256'));
    return $decoded;


}
}
